function msg(msg) {
    layer.msg(msg);
}

function remind(msg, callback) {
    layer.alert(msg, function() {
        if (callback) {
            callback();
        }
    });
}

function confirm(msg, confirmText, func) {
    layer.open({
        content: msg,
        btn: [confirmText, '取消'],
        yes: function(index) {
            func();
            layer.close(index);
        }
    });
}

/*上传图片到sm*/
function onUploadSMImage(file) {
    let formData = new FormData();
    formData.append('smfile', file.file);
    console.log(this)
    const loading = vue.$loading({
        lock: true,
        text: 'Loading',
        // spinner: 'el-icon-loading',
        // background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
            method: 'post',
            url: 'https://sm.ms/api/upload',
            data: formData,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
        .then(function(res) {
            // console.log(file)
            loading.close();
            file.onSuccess(res.data, file.file)
            return
        })
}

function replaceBr(str) {
    if (str == null) {
        return ''
    }
    var val = str + ''
    val = val.replace(/<br\/>/g, "\n");
    return val;
}

function replaceN(str) {
    if (str == null) {
        return ''
    }
    var val = str + ''
    // val = val.replace('\n', "<br/>")
    val = val.replace(/\n|\r\n/g,"<br/>");
    return val;
}

function loading() {
    // layer.open({ type: 2 });
    layer.load();
}

function endLoading() {
    layer.closeAll();
}


function axiosRequestPro(params, url, success, fail, complete) {
    var param = Qs.stringify(params);
    loading()
    axios({
            url: url,
            method: 'post',
            data: param,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        .then(function(response) {
            complete()
            endLoading();
            var status = response.data.status
            if (status == 200) {
                endLoading();
                // if (redirect == '') {
                //     window.location.reload()
                // } else {
                //     window.location.href = redirect
                // }
                success(response.data)
                // msg("登录成功");
            } else if (status == 204) {
                // msg(response.data.msg);
                vue.$alert(response.data.msg, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {}
                });
            } else {
                // msg(response.data.msg);
                fail(response.data)
                console.log(response.data);
            }
        })
        .catch(function(error) {
            complete()
            fail(error)
            endLoading();
            msg("请求失败");
            console.log(error);
        });
}

function axiosRequest(params, url, redirect) {
    var param = Qs.stringify(params);
    loading()
    axios({
            url: url,
            method: 'post',
            data: param,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
        .then(function(response) {
            endLoading();
            var status = response.data.status
            if (status == 200) {
                endLoading();
                if (redirect == '') {
                    window.location.reload()
                } else {
                    window.location.href = redirect
                }
                // msg("登录成功");
            } else if (status == 202) {
                window.location.reload()
            } else if (status == 204) {
                // msg(response.data.msg);
                vue.$alert(response.data.msg, '提示', {
                    confirmButtonText: '确定',
                    callback: action => {}
                });
            } else {
                msg(response.data.msg);
                console.log(response.data);
            }
        })
        .catch(function(error) {
            endLoading();
            // msg("请求失败");
            console.log(error);
        });
}


function aes(key, text) {
    var aesText = text;
    var key = CryptoJS.enc.Utf8.parse(key);
    var iv = CryptoJS.enc.Utf8.parse('RandomInitVector');
    aesText = CryptoJS.AES.encrypt(text, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    }).toString();
    return aesText;
}

function trimStrBr(text) {
    if (text == '') {
        return text;
    }
    text = text.replace(/\n/g, "</br>")
    text = text.replace(/\r/g, "</br>")
    return text
}

function trimStr(text) {
    if (text == '') {
        return text;
    }
    text = text.replace(/\n/g, " ")
    text = text.replace(/\t/g, " ")
    text = text.replace(/\r/g, " ")
    return text
}

function backBr(text) {
    if (text == 'text') {
        return text;
    }
    text = text.replace("<br/>", '\bn')
    return text
}


function toJson(text) {
    if (text == null) {
        return null
    }
    if (text == "") {
        return null
    }

    text = text.replace(/[\r]/g, "\\r")
    text = text.replace(/[\n]/g, "\\n")
    // text = text.replace(/\n/g, "<br/>")
    text = text.replace(/\t/g, " ")


    // text = text.replace(/↵/g, "<br/>");
    // text = text.replace('\"', "//n")
    // text = text.replace(/"/g, " ")
    // console.log(text)
    // console.log(text[267])
    return JSON.parse(text)
}

function toJson2(text) {
    if (text == null) {
        return null
    }
    if (text == "") {
        return null
    }
    text = text.replace(/[\r]/g, "\\r")
    text = text.replace(/[\n]/g, "\\n")
    return JSON.parse(text)

}